Re: Explain - Mailing list pgsql-general

From Heiko Irrgang
Subject Re: Explain
Date
Msg-id a04320404b67b41bbd16c@[192.168.66.11]
Whole thread Raw
In response to Explain  (Jeff Davis <jdavis@wasabimg.com>)
List pgsql-general
>How can I conveniently handle output from an EXPLAIN query inside an
>application? It appears to just immediately send the output to the stdout of
>whatever program uses it. Is there a way to get the output inside a program,
>with libpq or otherwise? I would actually like to be able to get the output in
>a PHP script, but getting it in any application would be nice.

AFAIK it dont works directly, but you can do the following:

<?php
$f=popen("psql -U username -c 'explain select * from table' -d dbname
2>/dev/stdout","r");

while($nextline=fgets($f,4096)) {
         echo($nextline."<br>");
}
pclose($f);
?>

this would dump the explain-result as you get it in psql as html-text.

with popen("command","r") you open a stream to the command and
because psql seems to send
the result of explain to stderr you have to redirect it to stdout
(2>/dev/stdout)

then you can handle the filedescriptor delivered by popen() as a normal file
with fgets(), but you have to close it with pclose() not fclose().

this works exactly the same within a c program but i dont know much
about programming postgres in c. it may be possible to have the
same result directly with libpq, but for php this seems to be
the only way to do this.

--
SC-Networks                             www: www.SC-Networks.de
Web Design, Netzwerke,
3D Animation und Multimedia
Heiko Irrgang                           Tel.: 08856/9392-00
Im Thal 2                               Fax:  08856/9392-01

82377 Penzberg                          Mail: Irrgang@SC-Networks.de

pgsql-general by date:

Previous
From: Ron Chmara
Date:
Subject: Re: How passwords can be crypted in postgres?
Next
From: Ron Chmara
Date:
Subject: Re: RE: RE: Re: MySQL and PostgreSQL speed compare